﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Assignment9.Models {

    /**
     **/
    public class Order : IComparable<Order>
    {

        public string ID { get => iD; set => iD = value; }
        public string iD;
        public Customer customer = new Customer("", false);
        public Time time = new Time(0, 0, 0, 0, 0);

        public List<OrderDetail> Details = new List<OrderDetail>();
        public static int number = 0;
        public double Amount { get => Customer.getIsVIP() ? 0.9 * Details.Sum(d => d.Amount) : Details.Sum(d => d.Amount); }
        public Customer Customer { get => customer; set => setCustomer(""); }
        public Time Time { set => setTime(); get => time; }
        //订单构造函数
        public Order() { }
        public Order(Customer c, List<OrderDetail> details, Time time)
        {
            customer = c;
            Details = details;
            this.time = time;
            ID = Math.Abs(customer.GetHashCode() + time.GetHashCode()).ToString();
        }

        //Customer get set
        public Customer GetCustomer() { return customer; }
        public void setCustomer(Customer c)
        {
            customer = c;
        }
        public void setCustomer(string s) { new Customer(s, false); }
        //Time get set
        public Time GetTime() { return Time; }
        public void setTime(Time t)
        {
            time = t;
        }
        public void setTime()
        {
            time = new Time(0, 0, 0, 0, 0);
        }
        //Order Details get

        public List<OrderDetail> GetOrderDetails()
        {
            return Details;
        }

        public void AddOrderDetails(OrderDetail d)
        {
            Details.Add(d);
        }

        public void RemoveDetails(int index)
        {
            Details.RemoveAt(index);
        }

        public OrderDetail ModifyDetails(int index, int q)
        {
            Details[index].Quantity = q;
            return Details[index];
        }
        //重写ToString方法，订单简单信息
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder("");
            foreach (OrderDetail d in Details) { stringBuilder.Append(d); stringBuilder.Append("\r\n"); }
            return $"Order time: {time}, Customer: {customer.getName()}\r\n" + stringBuilder.ToString();
        }

        //重写equals方法
        public override bool Equals(object obj)
        {
            Order order = (Order)obj;
            return order.Customer.getName().Equals(Customer.getName()) & order.Time.Equals(Time);
        }

        //重写GetHashCode方法
        public override int GetHashCode()
        {
            return Time.GetHashCode() + Customer.GetHashCode();
        }

        public int CompareTo(Order other)
        {

            if (other == null) return 1;
            return this.iD.CompareTo(other.iD);
        }
    }
}
